export class ImageUtils {
    public static convertFormat(
        imageData: Uint8ClampedArray,
        width: number,
        height: number,
        format: 'jpeg' | 'png' | 'webp',
        quality = 0.92
    ): string {
        const canvas = document.createElement('canvas');
        canvas.width = width;
        canvas.height = height;
        
        const ctx = canvas.getContext('2d')!;
        const img = new ImageData(imageData, width, height);
        ctx.putImageData(img, 0, 0);
        
        return canvas.toDataURL(`image/${format}`, quality);
    }
 
    public static async loadImage(url: string): Promise<ImageData> {
        const response = await fetch(url);
        const blob = await response.blob();
        const img = new Image();
        
        return new Promise((resolve, reject) => {
            img.onload = () => {
                const canvas = document.createElement('canvas');
                canvas.width = img.width;
                canvas.height = img.height;
                const ctx = canvas.getContext('2d')!;
                ctx.drawImage(img, 0, 0);
                resolve(ctx.getImageData(0, 0, img.width, img.height));
            };
            img.onerror = reject;
            img.src = URL.createObjectURL(blob);
        });
    }
}